{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tornado.web import StaticFileHandler, RedirectHandler, template\n",
    "from tornado import web\n",
    "import tornado\n",
    "\n",
    "\n",
    "class MainHandler(web.RequestHandler):\n",
    "    async def get(self, *args, **kwargs):\n",
    "        word = \"hello bobby\"\n",
    "        ########################################\n",
    "        ## 字符串模板\n",
    "        # t = template.Template(\"<h1>{{ word }}</h1>\")\n",
    "        # ## 注入模版参数\n",
    "        # self.finish(t.generate(word=word))\n",
    "\n",
    "        ########################################\n",
    "        ## 文件模板\n",
    "        loader = template.Loader(\"/path/to/templates\")\n",
    "        self.finish(loader.load(\"hello.html\").generate(word=word))\n",
    "\n",
    "        ########################################\n",
    "        ## self.render 加载模板\n",
    "        self.render(\"hello.html\", word=word)\n",
    "\n",
    "\n",
    "class MainHandler2(web.RequestHandler):\n",
    "    async def get(self, *args, **kwargs):\n",
    "        self.write(\"hello world2\")\n",
    "\n",
    "## 静态文件访问路径配置\n",
    "## 方法1\n",
    "settings = {\n",
    "    ## 静态文件的路径\n",
    "    \"static_path\": \"/Users/dengjun/Develop/python/tornado_demo/static\",\n",
    "    ## 静态文件的\n",
    "    \"static_url_prefix\": \"/static/\",\n",
    "    ## 模板目录\n",
    "    \"template_path\": \"templates\",\n",
    "}\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    app = web.Application([\n",
    "        (\"/\", MainHandler),\n",
    "        (\"/2/\", RedirectHandler, {\"url\": \"/\"}),\n",
    "        ## 静态文件访问路径配置\n",
    "        ## 方法2\n",
    "        (\"/static3/(.*)\", StaticFileHandler, {\"path\": \"/Users/dengjun/Develop/python/tornado_demo/static\"})\n",
    "    ], debug=True, **settings)\n",
    "\n",
    "    app.listen(8000)\n",
    "    tornado.ioloop.IOLoop.current().start()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 模板中静态文件的引用\n",
    "<link href=\"{{ static_url('css/public.css') }}\" type=\"text/css\" rel=\"stylesheet\" />\n",
    "\n",
    "## 引用外部函数\n",
    "{% from path.to.utils import cal_total %}\n",
    "\n",
    "## for 循环\n",
    "{% for order in orders %}\n",
    "    {{ order['name'] }}\n",
    "    {{ order['price'] }}\n",
    "    {{ order['nums'] }}\n",
    "    {{ cal_total(order['price'], order['nums']) }}\n",
    "{% end %}\n",
    "\n",
    "## if else\n",
    "{% if order['price'] > 0 %}\n",
    "    xxxx\n",
    "{% else %}\n",
    "    yyyy\n",
    "{% end %}\n",
    "\n",
    "## 设置变量\n",
    "{% set total = 0 %}\n",
    "\n",
    "## 特殊字符转义\n",
    "{% raw order['detail'] %}\n",
    "\n",
    "## 页面继承和重载\n",
    "# base.html\n",
    "{% block body %}\n",
    "{% end %}\n",
    "# 子页面\n",
    "{% extends \"base.html\" %}\n",
    "{% block body %}\n",
    "    ......\n",
    "    <div class=\"container\">子页面内容</div>\n",
    "    ......\n",
    "{% end %}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
